package com.miaoxun.dao.provider;

import com.miaoxun.domain.entity.UserRole;

import java.text.MessageFormat;
import java.util.List;
import java.util.Map;

public class UserRoleProvider {

    public String listRoleId(Long id) {
        StringBuilder sb = new StringBuilder();
        sb.append("select role_id from ");
        sb.append("user_role where ");
        sb.append("user_id=#{id}");
        return sb.toString();
    }

    public String removeByUserId(Long id) {
        StringBuilder sb = new StringBuilder();
        sb.append("delete    from ");
        sb.append("user_role where ");
        sb.append("user_id=#{id}");
        return sb.toString();
    }

    public String batchSave(Map map) {
        List<UserRole> list= (List<UserRole>) map.get("list");
        StringBuilder sb = new StringBuilder();
        sb.append("INSERT INTO user_role ");
        sb.append("(user_id, role_id) ");
        sb.append("VALUES ");
        MessageFormat mf = new MessageFormat("(#'{'list[{0}].userId}, #'{'list[{0}].roleId})");
        for (int i = 0; i < list.size(); i++) {
            sb.append(mf.format(new Object[]{i}));
            if (i < list.size() - 1) {
                sb.append(",");
            }
        }
        return sb.toString();

    }
}
